package com.huawei.M01.iterator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReplaceWords {
    public String evaluate(String s, List<List<String>> knowledge) {
        Map<String, String> knowMap = new HashMap<>();
        // 将knowledge转化为map
        for (List<String> strings : knowledge) {
            knowMap.put(strings.get(0), strings.get(1));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                StringBuilder stringBuilder = new StringBuilder();
                while (s.charAt(i + 1) != ')') {
                    stringBuilder.append(s.charAt(i + 1));
                    i++;
                }
                // 此时，s.charAt(i+1) = ')'
                String values = knowMap.getOrDefault(stringBuilder.toString(), "?");
                sb.append(values);
                i++;
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }
}
